import requests
import json
from apps.interface.client.dao import ClientDao

def getAccessToken(params):
    wxTokenUrl = "https://api.weixin.qq.com/cgi-bin/token"
    client_model = ClientDao().findAll().filter(clientname=params.get('clientname')).first()
    #设置参数
    params={}
    params["grant_type"]="client_credential"
    params["appid"]=client_model.appid
    params["secret"]=client_model.appsecret
    res = requests.get(wxTokenUrl, params=params)
    return json.loads(res.content)

def getOpenid(code, clientname):
    wxOpenidUrl = "https://api.weixin.qq.com/sns/jscode2session"
    #设置参数
    params={}
    params["js_code"]=code
    params["grant_type"]="authorization_code"
    client_model = ClientDao().findAll().filter(clientname=clientname).first()
    params["appid"]=client_model.appid
    params["secret"]=client_model.appsecret
    
    res = requests.get(wxOpenidUrl, params=params)
    resDict = json.loads(res.content)
    return resDict
    